<template>
  <view class="service-set-container">
    <view class="set-item-content">
      <view class="set-item">
        <view class="icon">
          <u-icon name="setting" color="#606266" size="36"></u-icon>
        </view>
        <view class="lable">快手广告模式</view>
        <view class="switch">
          <u-switch
            class="ks-model-switch"
            v-model="ksModelChecked"
            active-value="1"
            inactive-value="0"
            active-color="#56d39d"
            inactive-color="#eee"
            size="36"
            @change="changeKsModel">
          </u-switch>
        </view>
      </view>
    </view>

    <view class="set-item-content">
      <view class="set-item">
        <view class="icon">
          <u-icon name="setting" color="#606266" size="36"></u-icon>
        </view>
        <view class="lable">看广告下载案例视频（视频定制）</view>
        <view class="switch">
          <u-switch
            class="ks-model-switch"
            v-model="video_ad_download"
            active-value="1"
            inactive-value="0"
            active-color="#56d39d"
            inactive-color="#eee"
            size="36"
            @change="changeDownloadVideoModel">
          </u-switch>
        </view>
      </view>
    </view>

    <view class="set-item-content">
      <view class="set-item">
        <view class="icon">
          <u-icon name="setting" color="#606266" size="36"></u-icon>
        </view>
        <view class="lable">定制素材（IOS）看广告下载</view>
        <view class="switch">
          <u-switch
            class="ks-model-switch"
            v-model="make_ad_download"
            active-value="1"
            inactive-value="0"
            active-color="#56d39d"
            inactive-color="#eee"
            size="36"
            @change="changeDownloadMakeArtworksModel">
          </u-switch>
        </view>
      </view>
    </view>

    <view class="set-item-content">
      <view class="set-item">
        <view class="icon">
          <u-icon name="setting" color="#606266" size="36"></u-icon>
        </view>
        <view class="lable">图文定制素材看广告（非高清下载）</view>
        <view class="switch">
          <u-switch
            class="ks-model-switch"
            v-model="blur_download"
            active-value="1"
            inactive-value="0"
            active-color="#56d39d"
            inactive-color="#eee"
            size="36"
            @change="changeBlurDownloadMakeArtworksModel">
          </u-switch>
        </view>
      </view>
    </view>

    <view class="set-item-content">
      <view class="set-item">
        <view class="icon">
          <u-icon name="bell" color="#606266" size="36"></u-icon>
        </view>
        <view class="lable">支付通知</view>
        <view class="switch">
          <u-switch
            class="ks-model-switch"
            v-model="order_message"
            active-value="1"
            inactive-value="0"
            active-color="#56d39d"
            inactive-color="#eee"
            size="36"
            @change="changeOrderMessageModel">
          </u-switch>
        </view>
      </view>
    </view>

    <view class="set-item-content">
      <u-cell-group>
        <u-cell-item icon="file-text" @click="goAgreement" title="服务协议"></u-cell-item>
        <u-cell-item icon="more-circle" @click="goAbout" title="关于我们"></u-cell-item>
        <u-cell-item icon="minus-circle" @click="handleLogout" title="退出登录"></u-cell-item>
      </u-cell-group>
    </view>
  </view>
</template>

<script>
  import { getUserInfoById, updatekuaishouModel } from "@/api/user";
  export default {
    data() {
      return {
        userInfo: null,
        ksModelChecked: true,
        order_message: true,
        video_ad_download: true,
        make_ad_download: true,
        blur_download: true
      }
    },

    onLoad() {
      this.userInfoById();
    },

    methods: {

      // 非高清下载
      changeBlurDownloadMakeArtworksModel(val) {
        updatekuaishouModel({
          blur_download: val == 0 ? 0 : 1
        }).then(result => {
          if (result && result.code === 200) {
            this.userInfoById();
            uni.showToast({
              title: '操作成功',
              icon: 'none'
            })
          }
        })
      },

      // 切换快手模式
      changeKsModel(val) {
        updatekuaishouModel({
          ks_model: val == 0 ? 1 : 0
        }).then(result => {
          if (result && result.code === 200) {
            this.userInfoById();
            uni.showToast({
              title: '操作成功',
              icon: 'none'
            })
          }
        })
      },

      // 切换订单通知模式
      changeOrderMessageModel(val) {
        updatekuaishouModel({
          order_message: val == 0 ? 0 : 1
        }).then(result => {
          if (result && result.code === 200) {
            this.userInfoById();
            uni.showToast({
              title: '操作成功',
              icon: 'none'
            })
          }
        })
      },

      // 切换看广告下载案例视频
      changeDownloadVideoModel(val) {
        updatekuaishouModel({
          video_ad_download: val == 0 ? 0 : 1
        }).then(result => {
          if (result && result.code === 200) {
            this.userInfoById();
            uni.showToast({
              title: '操作成功,仅对苹果有效',
              icon: 'none'
            })
          }
        })
      },

      // 切换看广告下载定制作品
      changeDownloadMakeArtworksModel(val) {
        updatekuaishouModel({
          make_ad_download: val == 0 ? 0 : 1
        }).then(result => {
          if (result && result.code === 200) {
            this.userInfoById();
            uni.showToast({
              title: '操作成功,仅对苹果有效',
              icon: 'none'
            })
          }
        })
      },

      // 获取用户信息
      userInfoById() {
        getUserInfoById().then(result => {
          if (result && result.data) {
            let data = result.data;
            this.userInfo = data;
            this.ksModelChecked = data.authorInfo.ks_model == 1 ? false : true;
            this.order_message = data.authorInfo.order_message == 1 ? true : false;
            this.video_ad_download = data.authorInfo.video_ad_download == 1 ? true : false;
            this.make_ad_download = data.authorInfo.make_ad_download == 1 ? true : false;
            this.blur_download = data.authorInfo.blur_download == 1 ? true : false;
          }
        })
      },

      // 关于我们
      goAbout() {
        uni.navigateTo({ url: '/pages/about/about' })
      },

      // 前往用户协议
      goAgreement() {
        uni.navigateTo({
          url: '/pages/about/agreement'
        })
      },

      // 退出登陆
      async handleLogout() {
        let businessId = '';
        // #ifdef MP
        businessId = config.id;
        // #endif
        // #ifdef H5
        businessId = uni.getStorageSync('businessId');
        // #endif
        await this.$store.dispatch('setToken', '');
        uni.setStorageSync('token', '');
        uni.reLaunch({
          url: `/pages/mine/login?id=${businessId}`
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .service-set-container{
    padding: 32rpx;

    .set-item-content{
      background-color: #fff;
      border-radius: 16rpx;
      margin-bottom: 24rpx;

      .set-item{
        display: flex;
        padding: 28rpx 34rpx;
        align-items: center;

        .icon{
          width: 40rpx;
          display: flex;
          align-items: center;
        }
        .lable{
          font-size: 28rpx;
          flex: 1;
        }

        .switch{
          display: flex;
          align-items: center;
        }
      }
    }
  }
</style>
